#include "serial_info.h"
#include <QThread>
SerialClass::SerialClass()
{
    serial_port_ptr = new QSerialPort;
}

SerialClass::~SerialClass()
{
    delete serial_port_ptr;

}

void SerialClass::ScanSerial()
{
    qDebug() << "Controller's thread is :" << QThread::currentThreadId();
    QStringList serialStrList;
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        serial_port_ptr->setPort(info);
        // 下面判断所获取的端口是不是串口
        if(serial_port_ptr->open(QIODevice::ReadWrite)) {
            serialStrList.append(serial_port_ptr->portName());
            serial_port_ptr->close();
        }
    }
    serialPortList = serialStrList;
    emit scanResult(serialStrList);
//    return serialStrList;
}

/**
 * @brief SerialClass::OpenSerial
 * @param serialName 串口名称
 * @param baudRate 波特率
 * @param _databit 数据位:combox控件索引
 * @param _parity 校验位:combox控件索引
 * @param _stopbit 停止位:combox控件索引
 * @return
 */
void SerialClass::OpenSerial(QString serialName, int baudRate,
                             int _databit, int _parity,
                             int _stopbit)
{
    qDebug() << "Controller's thread is :" << QThread::currentThreadId();
    QSerialPort::DataBits databit;
    switch (_databit) {
    case 0: databit = QSerialPort::Data8;break;
    case 1: databit = QSerialPort::Data7;break;
    case 2: databit = QSerialPort::Data6;break;
    case 3: databit = QSerialPort::Data5;break;
    default: databit = QSerialPort::UnknownDataBits;
    }
    QSerialPort::Parity parity;
    switch (_parity) {
    case 0:parity = QSerialPort::NoParity;break;
    case 1:parity = QSerialPort::EvenParity;break;
    case 2:parity = QSerialPort::OddParity;break;
    case 3:parity = QSerialPort::SpaceParity;break;
    case 4:parity = QSerialPort::MarkParity;break;
    default: parity = QSerialPort::UnknownParity;
    }
    QSerialPort::StopBits stopbit;
    switch (_stopbit) {
    case 0: stopbit = QSerialPort::OneStop;break;
    case 1: stopbit = QSerialPort::OneAndHalfStop;break;
    case 2: stopbit = QSerialPort::TwoStop;break;
    default:stopbit = QSerialPort::UnknownStopBits;
    }
    qDebug()<<"Serial Open Info:"<<serialName<<baudRate<<databit<<parity<<stopbit;
    serial_port_ptr->setPortName(serialName);
    if (serial_port_ptr->open(QIODevice::ReadWrite)) {
        serial_port_ptr->setBaudRate(baudRate);
        serial_port_ptr->setDataBits(databit);
        serial_port_ptr->setDataBits(databit);
        serial_port_ptr->setParity(parity);
        serial_port_ptr->setStopBits(stopbit);
        serial_port_ptr->setFlowControl(QSerialPort::NoFlowControl);
        // 当下位机中有数据发送过来时就会响应这个槽函数
        connect(serial_port_ptr, SIGNAL(readyRead()), this, SLOT(readData()));
    }
}

void SerialClass::CloseSerial()
{
    serial_port_ptr->clear();
    serial_port_ptr->close();
}

void SerialClass::SendData(QByteArray &sendData)
{
    serial_port_ptr->write(sendData);
}

QByteArray SerialClass::hexStringToByteArray(QString HexString)
{
    bool ok;
    QByteArray ret;
    HexString = HexString.trimmed();
    HexString = HexString.simplified();
    QStringList sl = HexString.split(" ");

    foreach (QString s, sl)
    {
        if(!s.isEmpty())
        {
            char c = static_cast<char>(s.toInt(&ok, 16) & 0xFF);
            if(ok)
            {
                ret.append(c);
            }
            else
            {
                qDebug()<<"非法的16进制字符："<<s;
                QMessageBox::warning(nullptr, tr("错误："), QString("非法的16进制字符: \"%1\"").arg(s));
            }
        }
    }
    return ret;
}

QByteArray SerialClass::GetReadBuf()
{
    return readBuf;
}

void SerialClass::ClearReadBuf()
{
    readBuf.clear();
}

void SerialClass::readData()
{
    readBuf = serial_port_ptr->readAll();
    emit readSignal(readBuf);
//    qDebug()<<readBuf;
}
